import 'dart:developer';

import 'package:ad_flutter3105/tbad_flutter3105.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _tbAd = TbAd();

  @override
  void initState() {
    super.initState();
    initAd();
  }

  //初始化广告
  Future<void> initAd() async {
    log("initAd____");
    _tbAd.initAd(
      '1330509223779385377-7',
      onSuccess: () {
        log("initAd____onSuccess");
      },
      onFail: (String message) {
        log("initAd____onFail,message=$message");
      },
    );
    setState(() {});
    requestPermissionIfNecessary();
  }

  //申请动态权限
  Future<void> requestPermissionIfNecessary() async {
    log("RequestPermissionIfNecessary____RequestPermissionIfNecessary");
    _tbAd.requestPermissionIfNecessary();
    setState(() {});
  }

  //开屏广告
  Future<void> loadSplash() async {
    log("Splash____loadSplash");
    _tbAd.loadSplash(
      '1330512946933022784',
      onAdShow: (int eCPM) {
        log("Splash____onAdShow");
      },
      onAdClick: () {
        log("Splash____onAdClick");
      },
      onAdClose: () {
        log("Splash____onAdClose");
      },
      onError: (String message) {
        log("Splash____onError,message=$message");
      },
    );
    setState(() {});
  }

  //插屏广告
  Future<void> loadInteraction() async {
    log("Interaction____loadInteraction");
    _tbAd.loadInteraction(
      '1330513058157576258',
      onAdShow: (int eCPM) {
        log("Interaction____onAdShow");
      },
      onAdClick: () {
        log("Interaction____onAdClick");
      },
      onAdClose: () {
        log("Interaction____onAdClose");
      },
      onError: (String message) {
        log("Interaction____onError,message=$message");
      },
    );
    setState(() {});
  }

  //激励视频广告
  Future<void> loadRewardVideo() async {
    print("RewardVideo____loadRewardVideo");
    _tbAd.loadRewardVideo(
      "1330515047692124225",
      '传入userId或者设备唯一标识',
      '服务器回调额外信息',
      onAdShow: (int eCPM, String orderNo) {
        print("RewardVideo____onAdShow");
      },
      onReward: () {
        log("RewardVideo____onReward");
      },
      onAdClick: () {
        log("RewardVideo____onAdClick");
      },
      onAdClose: () {
        log("RewardVideo____onAdClose");
      },
      onError: (String message) {
        log("RewardVideo____onError,message=$message");
      },
    );
    setState(() {});
  }

  //短视频+广告
  Future<void> loadVideo() async {
    _tbAd.loadVideo('1400375964994650194');
    setState(() {});
  }

  //信息流广告
  Future<void> onFeedViewCreated(int id) async {
    //print("onFeedViewCreated____id=$id");
    final feed_channel = MethodChannel('com.tb.mob/feedview_$id');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 350,
              height: 200,
              color: Colors.red,
              child: AndroidView(
                viewType: 'com.tb.mob/feedview',
                creationParams: const {
                  "codeId": "1330513366195650582",
                  "width": 350,
                  "height": 200,
                },
                creationParamsCodec: const StandardMessageCodec(),
                onPlatformViewCreated: onFeedViewCreated,
              ),
            ),
            ElevatedButton(
              child: const Text('申请动态权限'),
              onPressed: () async {
                await requestPermissionIfNecessary();
              },
            ),
            ElevatedButton(
              child: const Text('开屏广告'),
              onPressed: () async {
                await loadSplash();
              },
            ),
            ElevatedButton(
              child: const Text('插屏广告'),
              onPressed: () async {
                await loadInteraction();
              },
            ),
            ElevatedButton(
              child: const Text('激励视频广告'),
              onPressed: () async {
                await loadRewardVideo();
              },
            ),
            ElevatedButton(
              child: const Text('短视频'),
              onPressed: () async {
                await loadVideo();
              },
            ),
          ],
        ),
      ),
    );
  }
}
